#!/bin/bash

test -z "${OS_NAME}" && OS_NAME=ubuntu:18.04
IMAGE=${OS_NAME}

test "${OS_NAME}" = "ubuntu:16.04" && IMAGE=ubuntu:xenial-20160422
test "${OS_NAME}" = "ubuntu:18.04" && IMAGE=ubuntu:bionic-20180426

function pack_in_docker()
{
    rm -rf /etc/apt/apt.conf.d/docker-clean
    MIRROR=mirrors.aliyun.com
    MIRROR=mirrors.huaweicloud.com

    # 修改软件源为国内的源镜像地址
    sed -i "s|archive.ubuntu.com|${MIRROR}|g;s|security.ubuntu.com|${MIRROR}|g;s|deb.debian.org|${MIRROR}|g;s|security.debian.org|${MIRROR}|g;" \
        /etc/apt/sources.list && apt-get update


    #添加 非官方打包软件的源
    #apt-get install -y --no-install-recommends apt-transport-https gnupg2 ca-certificates curl lsb-release

    # docker
    #curl -fsSL https://${MIRROR}/docker-ce/linux/ubuntu/gpg | apt-key add -
    #echo "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" >/etc/apt/sources.list.d/docker.list

    # cuda and cudnn
    OS_NAME=$(echo ${OS_NAME}|sed 's|:||g;s|\.||g')
    # shellcheck disable=SC2086
    #curl -fsSL https://mirrors.aliyun.com/nvidia-cuda/${OS_NAME}/x86_64/7fa2af80.pub | apt-key add -
    #echo "deb [allow-insecure=true trusted=true] https://mirrors.aliyun.com/nvidia-cuda/${OS_NAME}/x86_64/ /" > /etc/apt/sources.list.d/cuda.list && \
    #echo "deb [allow-insecure=true trusted=true] https://developer.download.nvidia.com/compute/machine-learning/repos/${OS_NAME}/x86_64/ /" >> /etc/apt/sources.list.d/cuda.list


    apt-get update
    PACKS=$* # 想要离线安装的软件包
    # shellcheck disable=SC2086
    apt-get install --yes --no-install-recommends ${PACKS} || exit 255
    apt-get install --yes --no-install-recommends dpkg-dev gzip #生成本地源的必要依赖

    PACK_DIR=/pack
    PWD=$(pwd)
    sed "s|@PACKAGE_NAMES@|$*|g" ${PWD}/install.sh >${PACK_DIR}/install.sh
    rm -rf ${PACK_DIR}/archives/{lock,partial}

    set -x
    ( cd ${PACK_DIR}; dpkg-scanpackages -m . > ${PACK_DIR}/Packages ; cat ${PACK_DIR}/Packages |gzip -r >Packages.gz; )
}

function pack()
{
    local PWD NAME
    PWD=`pwd`
    test -z "$1" && { echo "No package name"; exit 254;}
    # shellcheck disable=SC2086
    NAME=$(echo ${OS_NAME}-$*|sed "s|[ \t\r\n:]\+|_|g")
    PACK_DIR=pack_$(date +"%Y-%m-%d-%H")
    mkdir -p "${PACK_DIR}/archives";
    # shellcheck disable=SC2086
    docker run -it --rm -v "${PWD}/${PACK_DIR}":/pack \
            -v "${PWD}/${PACK_DIR}/archives":/var/cache/apt/archives \
            -v "${PWD}":"${PWD}" \
            --workdir "${PWD}" -e OS_NAME=${OS_NAME} ${IMAGE} bash build_repo pack_in_docker $* \
    && zip -r ${NAME}.zip ${PACK_DIR} && echo "" && echo "Offline Package: ${NAME}.zip"
}

test -n "$1" && $*
